const {
  WechatFastWatcher
} = require("./Watcher")

class TaskManage {
  constructor() {
    this.taskList = {};
    this.watcherList = [];
  }

  init(watcherList) {
    let self = this;
    this.watcherList = watcherList;
    this.watcherList.forEach(x => {
      x.onTime = function(sender) {
        var taskList = self.taskList;
        // console.log(taskList);
        for (var variable in taskList) {
          if (taskList.hasOwnProperty(variable)) {
            var task = taskList[variable];
            if (task.taskType == sender.taskType) {
              task.selfCheck();
            }
          }
        }
      }
      x.start();
    });
  }

  add(task) {
    this.taskList[task.orderId] = task;
  }
  start() {

  }
  generate(task) {}
  addWatcher() {}
  remove() {}
  removeByTaskId(taskId) {}
  stopWatcher() {}
  saveToDB() {}
  loadFromDB() {}
  removeTaskByOrderId(orderId) {
    delete this.taskList[orderId];
  }
  removeTask(task) {
    delete this.taskList[task.orderId];
  }
}

const taskManage = new TaskManage();
const wechatFastWatcher = new WechatFastWatcher();
const wechatFastWatcherList = [wechatFastWatcher]
taskManage.init(wechatFastWatcherList);


module.exports = taskManage
